package ihisab



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import ihisab.command.DailyReport

@Transactional(readOnly = true)
class DailyReportController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
		//Expense
		Date date = new Date()  
  
		// Get Calendar object set to the date and time of the given Date object  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
  
		// Set time fields to zero  
		cal.set(Calendar.HOUR_OF_DAY, 0);  
		cal.set(Calendar.MINUTE, 0);  
		cal.set(Calendar.SECOND, 0);  
		cal.set(Calendar.MILLISECOND, 0);  
  
		// Put it back in the Date object  
		date = cal.getTime();
		List expenseFromDateList=LendingTransaction.findAllByFromDateAndTransactionType(date, "CREDIT")
		List expenseToDateList= LendingTransaction.findAllByToDateAndTransactionType(date,"CREDIT")
		def expenseResult=expenseFromDateList + expenseToDateList
		BigDecimal expenseSumAmount=new BigDecimal("0.0") 
		BigDecimal expenseSumInterestAmount=new BigDecimal("0.0") 
		expenseResult.each{
			expenseSumAmount=expenseSumAmount+new BigDecimal(it.amount)
			expenseSumInterestAmount=expenseSumInterestAmount+new BigDecimal(it.interestAmount)
		}
		
		//Income
		List incomeFromDateList=LendingTransaction.findAllByFromDateAndTransactionType(date, "DEBIT")
		List incomeToDate= LendingTransaction.findAllByToDateAndTransactionType(date,"DEBIT")
		def incomeResult=incomeFromDateList
		BigDecimal incomeSumAmount=new BigDecimal("0.0")
		BigDecimal incomeSumInterestAmount=new BigDecimal("0.0")
		incomeResult.each{
			incomeSumAmount=incomeSumAmount+new BigDecimal(it.amount)
			incomeSumInterestAmount=incomeSumInterestAmount+new BigDecimal(it.interestAmount)
		}
		
		List dailyReportInstanceList=new ArrayList()
		//capital entry
		DailyReport dailyReport=new DailyReport(mode:"CAPITAL",incomeAmount:incomeSumAmount,expenseAmount:expenseSumAmount,netAmount:(incomeSumAmount-expenseSumAmount))
		dailyReportInstanceList.add(dailyReport)
		//interest entry
		dailyReport=new DailyReport(mode:"INTEREST",incomeAmount:incomeSumInterestAmount,expenseAmount:expenseSumInterestAmount,netAmount:(incomeSumInterestAmount-expenseSumInterestAmount))
		dailyReportInstanceList.add(dailyReport)
		
		
		
        //respond resultList, model:[dailyReportInstanceCount: 2]
		render (view:'index' ,model:[dailyReportInstanceList:dailyReportInstanceList])
    }
	
	
	

	
	def create() {
		respond new DailyReport(params)
	}

    /*
   def show(DailyReport dailyReportInstance) {
        respond dailyReportInstance
    }

    def create() {
        respond new DailyReport(params)
    }

    @Transactional
    def save(DailyReport dailyReportInstance) {
        if (dailyReportInstance == null) {
            notFound()
            return
        }

        if (dailyReportInstance.hasErrors()) {
            respond dailyReportInstance.errors, view:'create'
            return
        }

        dailyReportInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.created.message', args: [message(code: 'dailyReportInstance.label', default: 'DailyReport'), dailyReportInstance.id])
                redirect dailyReportInstance
            }
            '*' { respond dailyReportInstance, [status: CREATED] }
        }
    }

    def edit(DailyReport dailyReportInstance) {
        respond dailyReportInstance
    }

    @Transactional
    def update(DailyReport dailyReportInstance) {
        if (dailyReportInstance == null) {
            notFound()
            return
        }

        if (dailyReportInstance.hasErrors()) {
            respond dailyReportInstance.errors, view:'edit'
            return
        }

        dailyReportInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'DailyReport.label', default: 'DailyReport'), dailyReportInstance.id])
                redirect dailyReportInstance
            }
            '*'{ respond dailyReportInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(DailyReport dailyReportInstance) {

        if (dailyReportInstance == null) {
            notFound()
            return
        }

        dailyReportInstance.delete flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'DailyReport.label', default: 'DailyReport'), dailyReportInstance.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'dailyReportInstance.label', default: 'DailyReport'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }*/
}
